import {defineStore} from "pinia"
import {doGet} from "@/config/http"
import {svc} from '@/config/common'
import useDictStore from "@/store/modules/dict";

const useEnumsStore = defineStore(
'enums',
    {
        state: () => ({
            enums: {},
        }),
        actions: {
            getEnums(enumType) {
                if (!enumType) {
                    return null
                }
                if (this.enums[enumType] !== undefined) {
                    console.debug(enumType + '存在，不调接口')
                    return this.enums[enumType]
                }
                console.log(enumType + '不存在，调接口')
                this.initEnums(enumType)
                return this.enums[enumType]
            },
            setEnums(key, value) {
                if (key !== null && key !== "") {
                    // this.enums.push({
                    //     key: key,
                    //     value: value
                    // });
                    this.enums[key] = value
                }
            },
            initEnums(enumType) {
                if (this.enums[enumType] !== undefined) {
                    return
                }
                doGet(svc, '/common/enums', {type: enumType}, false).then(res => {
                    this.enums[enumType] = res
                })
            },
            initAllEnums() {
                doGet(svc, '/common/enums/multi', {}, false).then(res => {
                    this.enums = res
                    console.debug('enums', this.enums)
                })
            },
        }
    })

export default useEnumsStore
